package io.renren.wh.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.wh.channel.dao.ChannelInfoDao;
import io.renren.wh.channel.entity.ChannelInfoEntity;
import io.renren.wh.channel.service.ChannelInfoService;
import io.renren.wh.channel.vo.ChannelInfoSaveVo;
import io.renren.wh.interfaces.dao.InterfacesDao;
import io.renren.wh.interfaces.entity.InterfacesEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service("channelInfoService")
public class ChannelInfoServiceImpl extends ServiceImpl<ChannelInfoDao, ChannelInfoEntity> implements ChannelInfoService {


    @Autowired
    private InterfacesDao interfacesDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<ChannelInfoEntity> queryWrapper = new QueryWrapper<>();

        if (params != null) {
            String channelname = (String) params.get("channelname");
            if (!StringUtils.isEmpty(channelname)) {
                queryWrapper.eq("channelName",channelname);
            }
            String interfacename = (String) params.get("interfacename");
            if (!StringUtils.isEmpty(interfacename)) {
                queryWrapper.eq("interfaceName",interfacename);
            }
            String status = (String) params.get("status");
            if (!StringUtils.isEmpty(status)) {
                queryWrapper.eq("status",status);
            }
            String interfaceId = (String) params.get("interfaceId");
            if (!StringUtils.isEmpty(interfaceId)) {
                queryWrapper.eq("interfaceId",interfaceId);
            }
        }

        IPage<ChannelInfoEntity> page = this.page(
                new Query<ChannelInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public ChannelInfoEntity selectByInterfaceName(String interfaceName) {

        ChannelInfoEntity channelInfoEntity = this.baseMapper.selectOne(new QueryWrapper<ChannelInfoEntity>().eq("interfaceName", interfaceName));

        return channelInfoEntity;
    }

    @Override
    public void insert(ChannelInfoSaveVo vo) {

        ChannelInfoEntity channelInfoEntity = new ChannelInfoEntity();
        BeanUtils.copyProperties(vo,channelInfoEntity);

        InterfacesEntity interfaceInfo = interfacesDao.selectById(vo.getInterfacename());

        channelInfoEntity.setInterfaceid(interfaceInfo.getId());
        channelInfoEntity.setInterfacecode(interfaceInfo.getInterfacecode());
        channelInfoEntity.setInterfacename(interfaceInfo.getInterfacename());
        this.baseMapper.insert(channelInfoEntity);

    }

    @Override
    public void modify(ChannelInfoSaveVo vo) {

        ChannelInfoEntity channelInfoEntity = new ChannelInfoEntity();
        BeanUtils.copyProperties(vo,channelInfoEntity);

        InterfacesEntity interfaceInfo = null;

        try {
            interfaceInfo = interfacesDao.selectOne(new QueryWrapper<InterfacesEntity>().eq("interfaceName",vo.getInterfacename()));
        } catch (Exception e) {
        }

        if (interfaceInfo == null) {
            interfaceInfo = interfacesDao.selectById(vo.getInterfacename());
        }

        channelInfoEntity.setInterfaceid(interfaceInfo.getId());
        channelInfoEntity.setInterfacecode(interfaceInfo.getInterfacecode());
        channelInfoEntity.setInterfacename(interfaceInfo.getInterfacename());
        channelInfoEntity.setId(vo.getId());

        this.baseMapper.updateById(channelInfoEntity);

    }

}